package com.soikc.kuaidiE.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.soikc.kuaidiE.bean.Courier;
import com.soikc.kuaidiE.bean.Message;
import com.soikc.kuaidiE.mvc.ResponseBody;
import com.soikc.kuaidiE.service.CourierService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class CourierWeb {
    @ResponseBody("/api/courier/list.do")
    public String getCourierList(HttpServletRequest req, HttpServletResponse res){
        List<Courier> list = null;
        List<Courier> all = CourierService.queryList();
        try {
            //获取参数
            int offset = Integer.parseInt(req.getParameter("offset"));
            int pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
            list = CourierService.queryList(offset, pageNumber);
        } catch (Exception e) {
            list = all;
            e.printStackTrace();
        }
        JSONArray arr = new JSONArray();
        list.forEach(d -> {
            arr.add(d.toViveObj());
        });
        JSONObject data = new JSONObject();
        data.put("rows", arr);
        data.put("total", all.size());
        return data.toJSONString();
    }
    @ResponseBody("/api/courier/insert.do")
    public String addUser(HttpServletRequest req, HttpServletResponse res){
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String card_id = req.getParameter("card_id");
        String mail = req.getParameter("mail");
        String password = req.getParameter("password");
        Courier courier = new Courier();
        courier.setNickname(name);
        courier.setPhone(phone);
        courier.setMail(mail);
        courier.setId_card(card_id);
        courier.setPassword(password);
        boolean b = CourierService.add(courier);
        Message message = new Message(b ? 0 : -1, "添加" + (b ? "成功" : "失败"));
        return message.toString();
    }
    @ResponseBody("/api/courier/find.do")
    public String queryPhone(HttpServletRequest req, HttpServletResponse res){
        String phone = req.getParameter("phone");
        Message message = new Message(-1,"手机号错误,请输入11位手机号");
        if (phone!=null&&phone.length()==11){
            Courier courier = CourierService.queryPhone(phone);
            if (courier!=null){
                message.setStatus(0);
                message.setResult("查询成功");
                message.setData(courier.toViveObj());
            }else {
                message.setResult("查询失败");
            }
        }
        return message.toString();
    }
    @ResponseBody("/api/courier/update.do")
    public String update(HttpServletRequest req, HttpServletResponse res){
        String id = req.getParameter("id");
        String nickname = req.getParameter("name");
        String phone = req.getParameter("phone");
        String mail = req.getParameter("mail");
        String password = req.getParameter("password");
        Courier courier = new Courier();
        courier.setNickname(nickname);
        courier.setPhone(phone);
        courier.setMail(mail);
        if (password!=null&&password.length()>0) {
            courier.setPassword(password);
        }
        boolean b = CourierService.update(Integer.parseInt(id),courier);
        Message message = new Message(b ? 0 : -1, "修改" + (b ? "成功" : "失败"));
        return message.toString();
    }
    @ResponseBody("/api/courier/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse res){
        String id = req.getParameter("id");
        boolean b = false;
        try {
            b = CourierService.delID(Integer.parseInt(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Message message = new Message(b ? 0 : -1, "删除" + (b ? "成功" : "失败"));
        return message.toString();
    }
}
